package com.zxy.express.service.impl;

import com.autopai.base.common.mybatis.service.impl.BaseServiceImpl;
import com.autopai.base.common.web.constant.Constant;
import com.autopai.base.common.web.convert.ConvertUtils;
import com.autopai.base.common.web.page.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxy.express.dao.AudioEpisodeDao;
import com.zxy.express.dto.AudioEpisodeDTO;
import com.zxy.express.entity.AudioEpisodeEntity;
import com.zxy.express.service.AudioEpisodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 合集下的单集
 *
 * @author zhangxiying ${email}
 * @since ${version} 2025-07-01
 */
@Service
public class AudioEpisodeServiceImpl extends BaseServiceImpl<AudioEpisodeDao, AudioEpisodeEntity> implements AudioEpisodeService {

    @Override
    public PageData<AudioEpisodeDTO> page(Map<String, Object> params) {
        IPage<AudioEpisodeEntity> page = baseDao.selectPage(
                getPage(params, "sort", true),
                getWrapper(params)
        );

        return getPageData(page, AudioEpisodeDTO.class);
    }

    @Override
    public List<AudioEpisodeDTO> list(Map<String, Object> params) {
        List<AudioEpisodeEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, AudioEpisodeDTO.class);
    }

    private QueryWrapper<AudioEpisodeEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AudioEpisodeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        String collectionId=(String) params.get("collectionId");
        wrapper.eq(StringUtils.isNotEmpty(collectionId),"collection_id",collectionId);
        return wrapper;
    }

    @Override
    public AudioEpisodeDTO get(String id) {
        AudioEpisodeEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, AudioEpisodeDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AudioEpisodeDTO dto) {
        AudioEpisodeEntity entity = ConvertUtils.sourceToTarget(dto, AudioEpisodeEntity.class);

        insert(entity);
    }

    @Override
    public void update(AudioEpisodeDTO dto) {
        AudioEpisodeEntity entity = ConvertUtils.sourceToTarget(dto, AudioEpisodeEntity.class);

        updateById(entity);
    }

    @Override
    public void updateByTitle(AudioEpisodeDTO dto) {
        UpdateWrapper<AudioEpisodeEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("timed_content",dto.getTimedContent());
        wrapper.eq("title", dto.getTitle());
        baseDao.update(null, wrapper);
    }

    @Override
    public void delete(String[] ids) {
        //逻辑删除
        //logicDelete(ids, MaterialEntity.class);

        //物理删除
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<AudioEpisodeDTO> dtoList) {
        if (dtoList == null || dtoList.isEmpty()) {
            return;
        }
        insertBatch(ConvertUtils.sourceToTarget(dtoList, AudioEpisodeEntity.class));
    }

    @Override
    public AudioEpisodeDTO getNext(String id) {
        AudioEpisodeDTO currentDTO=get(id);
        if(currentDTO!=null){
            QueryWrapper<AudioEpisodeEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("collection_id",currentDTO.getCollectionId());
            wrapper.gt("sort",currentDTO.getSort());
            wrapper.orderByAsc("sort");
            wrapper.last("limit 1");
            AudioEpisodeEntity entity = baseDao.selectOne(wrapper);
            if(entity!=null){
                return ConvertUtils.sourceToTarget(entity, AudioEpisodeDTO.class);
            }
        }
        return null;
    }

    @Override
    public AudioEpisodeDTO getPrevious(String id) {
        AudioEpisodeDTO currentDTO=get(id);
        if(currentDTO!=null){
            QueryWrapper<AudioEpisodeEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("collection_id",currentDTO.getCollectionId());
            wrapper.lt("sort",currentDTO.getSort());
            wrapper.orderByDesc("sort");
            wrapper.last("limit 1");
            AudioEpisodeEntity entity = baseDao.selectOne(wrapper);
            if(entity!=null){
                return ConvertUtils.sourceToTarget(entity, AudioEpisodeDTO.class);
            }
        }
        return null;
    }

}